#include<cstdio>
#include<iostream>
#include<my_global.h>
#include<mysql.h>
#include<unistd.h>
#include"functions.h"
extern MYSQL *mysql;
/*int sys_1_search()
{
	char sc;
	if(mysql_query(mysql, "select * from tb1"))
	{
		printf("查询错误！\n");
		sleep(5);
		return 0;
	}
	MYSQL_RES *result = mysql_store_result(mysql);
	if(result == NULL)
	{
		fprintf(stderr, "%s\n", mysql_error(mysql));
		sleep(5);
		return 0;
	}
	int num_fields = mysql_num_fields(result);
	MYSQL_ROW row;
	while((row = mysql_fetch_row(result)))
	{
		for(int i = 0; i < num_fields; i++)
		{
			printf("%s ", row[i] ? row[i] : "NULL");
		}
		printf("\n");
	}
	printf("回车退出!\n");
	scanf("%c", &sc);
	scanf("%c", &sc);
	mysql_free_result(result);
	return 1;
}
int sys_1_insert()
{
	char sc;
	char mysql_insert[100];
	char name[20];
	int ID,age;
	printf("请输入要添加的学生信息：学号、姓名、年龄,以空格为分隔:");
	scanf("%d %s %d", &ID ,name , &age);
	sprintf(mysql_insert,"INSERT INTO tb1 (id,name,age) values('%d','%s','%d')", ID, name, age);
	if(!sys_1_error(mysql_insert))
		return 0;
	printf("输入回车结束\n");
	scanf("%c", &sc);	scanf("%c", &sc);
	return 1;
}*/
/*int sys_1_drop()
{
	char sc;
	int flag;
	char mysql_delete[100];
	printf("请输入删除成员的关键字段:1、ID 2、姓名 3、年龄 4、返回:");
	scanf("%d", &flag);
	switch(flag)
	{
		case 1:
			int ID;
			printf("请输入您要删除的学生ID号:");
			scanf("%d", &ID);
			sprintf(mysql_delete, "DELETE FROM tb1 WHERE ID='%d'", ID);
			if(!sys_1_error(mysql_delete))
					return 0;
			break;
		case 2:
			char name[20];
			printf("请输入您要删除的学生姓名:");
			scanf("%s", name);
			sprintf(mysql_delete, "DELETE FROM tb1 WHERE name='%s'", name);
			if(!sys_1_error(mysql_delete))
				return 0;
			break;
		case 3:
			int age;
			printf("请输入您要删除的学生年龄:");
			scanf("%d", &age);
			sprintf(mysql_delete, "DELETE FROM tb1 WHERE age='%d'", age);
			if(!sys_1_error(mysql_delete))
				return 0;
			break;
		case 4:
			return 1;
			break;
	}
	printf("输入回车结束\n");
	scanf("%c", &sc); scanf("%c", &sc);
	return 1;
}
*/
int sys_1_modify()
{
	char sc;
	int ID, flag, age, n_id;
	char name[50], mysql_modify[100];
	printf("请输入您要修改的学生ID：");
	scanf("%d", &ID);
	sprintf(mysql_modify, "SELECT * FROM tb1 WHERE ID='%d'", ID);
	mysql_query(mysql, mysql_modify);
	MYSQL_RES *res = mysql_store_result(mysql);
	if(mysql_num_rows(res) == 0)
	{	
		fprintf(stderr, "%s\n", mysql_error(mysql));
		printf("无此学生！\n");
		sleep(5);
		return 0;
	}
	else
	{
		printf("您要修改的学生信息是:");
		MYSQL_ROW row = mysql_fetch_row(res);
		for(unsigned int i = 0; i < mysql_num_fields(res); i++)
		{
			printf("%s ",row[i]);
		}
		mysql_free_result(res);
		printf("\n请输入您要修改的学生信息：1、学号 2、姓名 3、年龄 4、全部修改 5、返回:");
		scanf("%d", &flag);
		switch(flag)
		{
			case 1:
				printf("请输入新的学号:");
				scanf("%d", &n_id);
				sprintf(mysql_modify, "UPDATE tb1 SET ID='%d' WHERE ID='%d'", n_id, ID);
				if(!sys_error(mysql_modify))
					return 0;
				break;
			case 2:
				printf("请输入新的姓名:");
				scanf("%s", name);
				sprintf(mysql_modify, "UPDATE tb1 SET name='%s' WHERE ID='%d'", name, ID);
				if(!sys_error(mysql_modify))
					return 0;
				break;
			case 3:
				printf("请输入新的年龄:");
				scanf("%d", &age);
				sprintf(mysql_modify, "UPDATE tb1 SET age='%d' WHERE ID='%d'", age, ID);
				if(!sys_error(mysql_modify))
					return 0;
				break;
			case 4:
				printf("请输入新的学号、姓名和年龄,以空格为分隔:");
				scanf("%d %s %d", &n_id, name, &age);
				sprintf(mysql_modify, "UPDATE tb1 SET ID='%d',name='%s',age='%d' WHERE ID='%d'", n_id, name, age, ID);
				if(!sys_error(mysql_modify))
					return 0;
				break;
			case 5:
				return 1;
				break;
		}
	}
	printf("输入回车结束 ");
	scanf("%c", &sc); scanf("%c", &sc);
	return 1;
}

